package com.sanjin.controller;

import com.sanjin.entity.ParkOrder;
import com.sanjin.entity.ParkSpace;
import com.sanjin.entity.WxMiniUser;
import com.sanjin.enums.ParkSpaceStatusEnum;
import com.sanjin.exception.MyException;
import com.sanjin.service.ParkOrderService;
import com.sanjin.service.ParkSpaceService;
import com.sanjin.service.WxMiniUserService;
import com.sanjin.utils.ResponseVoUtils;
import com.sanjin.utils.SecurityUtils;
import com.sanjin.utils.ValidateUtils;
import com.sanjin.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/10 10:23
 */
@Api(tags = "微信小程序用户业务")
@RestController
@RequestMapping("/wxMini")
public class WxMiniUserController{

    @Autowired
    private WxMiniUserService wxMiniUserService;

    @Autowired
    private ParkSpaceService parkSpaceService;

    @Autowired
    private ParkOrderService orderService;

    @Autowired
    private SecurityUtils securityUtils;

    @ApiOperation(value = "获取所有微信用户信息")
    @GetMapping("/list")
    public ResponseVo getAllWxMiniUsers() {
        List<WxMiniUser> wxMiniUsers = wxMiniUserService.queryWxMiniUsers();
        return ResponseVoUtils.success(wxMiniUsers);
    }

    @ApiOperation(value = "绑定小程序用户的车牌号码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carLicenseNumber", value = "车牌号码（需要符合规范）", paramType = "query")
    })
    @PostMapping("/bindCarLicenseNumber")
    public ResponseVo bindCarLicenseNumber(@RequestParam String carLicenseNumber) {
        // 验证车牌号合法性
        if (!ValidateUtils.valideteCarLicenseNumber(carLicenseNumber)) {
            throw new MyException("您的车牌号不符合规范，请重新填写");
        }
        WxMiniUser wxMiniUser = securityUtils.getWxMiniUser();
        wxMiniUser.setCarLicenseNumber(carLicenseNumber);
        wxMiniUserService.updateWxMiniUser(wxMiniUser);
        return ResponseVoUtils.success(wxMiniUser);
    }

//    @ApiOperation(value = "小程序用户选择停车车位")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "parkSpaceId", value = "车位id", paramType = "query")
//    })
//    @PostMapping("/chooseParkSpace")
//    public ResponseVo chooseParkSpace(@RequestParam Integer parkSpaceId) {
//        // 将用户选中的车位设置为 已使用 状态
//        ParkSpace parkSpace = parkSpaceService.changeParkSpaceStatus(parkSpaceId, ParkSpaceStatusEnum.APPYLY.code);
//        return ResponseVoUtils.success(parkSpace);
//    }

    @ApiOperation(value = "获取小程序用户信息")
    @GetMapping("/getUserInfo")
    public ResponseVo getUserInfo() {
        WxMiniUser wxMiniUser = securityUtils.getWxMiniUser();
        return ResponseVoUtils.success(wxMiniUser);
    }

    @ApiOperation(value = "获取用户订单信息")
    @GetMapping("/getUserOrder")
    public ResponseVo getUserOrder() {
        WxMiniUser wxMiniUser = securityUtils.getWxMiniUser();
        // 查询未支付订单
        ParkOrder parkOrder = orderService.queryNonPayOrderByWxMiniUserId(wxMiniUser.getId());

        return ResponseVoUtils.success(parkOrder);
    }
}
